from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey
from time import  sleep

desired_caps = {
    'platformName': 'Android', # 被测手机是安卓
    'platformVersion': '10', # 手机安卓版本
    'deviceName': 'chn', # 设备名，安卓手机可以随意填写
    'appPackage': 'com.dianping.v1', # 启动APP Package名称
    'appActivity': '.NovaMainActivity', # 启动Activity名称
    'unicodeKeyboard': True, # 使用自带输入法，输入中文时填True
    'resetKeyboard': True, # 执行完程序恢复原来输入法
    'noReset': True,       # 不要重置App
    'newCommandTimeout': 6000,
    'automationName' : 'UiAutomator2'
    # 'app': r'd:\apk\bili.apk',
}

class text(object):
    def __init__(self,introduction,username):
        self.introduction = introduction
        self.username = username

    def show(self):
        print("用户名: {} \n 主题: {}".format(self.username,self.introduction))




def get_Size():
    x = driver.get_window_size()['width']  # 获取x轴的长度
    y = driver.get_window_size()['height']  # 获取y轴的长度
    return x, y


def swipeDown():
    size = get_Size()
    x1 = int(size[0] * 0.5)  # size[0]取元组的第一个值，*0.5表示中间的点
    y1 = int(size[1] * 3 / 4)  # size[1]取元组的第二个值，*0.1表示距离底部近
    y2 = int(size[1] * 1 / 6)
    sleep(2)
    driver.swipe(x1, y1, x1, y2, 1000)

# 连接Appium Server，初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.implicitly_wait(5)
#
# driver.find_element_by_id("com.dianping.v1:id/home_tab_home").click()
#
# sleep(1)

text_list = []

# while(1):

n=0
while n<10:
    swipeDown()
    elements_text = driver.find_elements_by_xpath(
        '//android.support.v7.widget.RecyclerView[@resource-id="com.dianping.v1:id/recycler_view"]/*/*/*/android.widget.TextView[position()<=2]')

    for i in range(len(elements_text)//2):
        user_text = text(elements_text[i*2].text,elements_text[i*2+1].text)
        text_list.append(user_text)
    n+=1

for i in text_list:
    i.show()
